package com.study.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.study.domain.EmqxClientEvents;
import com.study.service.EmqxClientEventsService;
import com.study.service.impl.MqttMessageSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Timestamp;


/**
 * @Description: TODO
 * @Author: csc
 * @Date: 2024/12/4 10:33
 * @Version: 1.0
 **/
@RestController
@RequestMapping("webhook")
public class DeviceController {


    @Autowired
    private EmqxClientEventsService  emqxClientEventsService;

    @PostMapping("/device/onlineandoffline")
    public void test(@RequestBody String str){
        System.out.println("你好"+str);
        JSONObject parse = JSON.parseObject(str);
        long timestamp = parse.getLongValue("timestamp");
        EmqxClientEvents events = new EmqxClientEvents();
        events.setTs(new Timestamp(timestamp));
        events.setEvent(parse.getString("event"));
        events.setClientid(parse.getString("clientid"));

        boolean save = emqxClientEventsService.save(events);
        System.out.println("这是什么: "+save);
    }


    @Resource
    private MqttMessageSender mqttMessageSender;




    @GetMapping("/app")
    public void app(){
        mqttMessageSender.send("/yy", "迪迦奥特曼");
    }


}
